package com.foolishbird.sdk.core;


import com.foolishbird.sdk.handler.AsyncResponseHandler;
import com.foolishbird.sdk.handler.ResponseHanderInterface;
import com.foolishbird.sdk.params.BaseRequestParams;
import com.foolishbird.sdk.params.BindIDCardReuqestParams;
import com.foolishbird.sdk.params.BindPhoneReuqestParams;
import com.foolishbird.sdk.params.ConfigInfoRequestParams;
import com.foolishbird.sdk.params.CreateOrderRequestParams;
import com.foolishbird.sdk.params.LoginAutoRequestParams;
import com.foolishbird.sdk.params.LoginPhoneRequestParams;
import com.foolishbird.sdk.params.LoginRequestParams;
import com.foolishbird.sdk.params.LogoutRequestParams;
import com.foolishbird.sdk.params.ModifyPasswordReuqestParams;
import com.foolishbird.sdk.params.PayChannelReuqestParams;
import com.foolishbird.sdk.params.PostRequestParams;
import com.foolishbird.sdk.params.QuickRegisterRequestParams;
import com.foolishbird.sdk.params.RegisterRequestParams;
import com.foolishbird.sdk.params.RoleInfoRequestParams;
import com.foolishbird.sdk.params.SendVerifyReuqestParams;
import com.foolishbird.sdk.utils.WL;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public final class HttpEngine {

    private static final String TAG = HttpEngine.class.getSimpleName();

    ExecutorService httpThreadPool;

    HashMap<Integer, HttpEngineTask> tasks;

    HttpEngine(int threadPoolSize) {
        if(threadPoolSize <= 1){
            httpThreadPool = Executors.newSingleThreadExecutor();
        }else{
            httpThreadPool = Executors.newFixedThreadPool(threadPoolSize);
        }
        tasks = new HashMap<Integer, HttpEngineTask>();
    }


    public void shutdown(){
        if(httpThreadPool != null && !httpThreadPool.isShutdown()){
            httpThreadPool.shutdown();
            httpThreadPool = null;
            WL.d(TAG, "Destroy HttpEngine thread pool");
        }
    }

    public boolean isShutdown(){
        if(httpThreadPool == null){
            return true;
        }
        return httpThreadPool.isShutdown();
    }

    protected int submitTask(final HttpEngineTask task){
        if(httpThreadPool != null && !httpThreadPool.isShutdown()){
            final int taskId = task.getCurrentTaskId();
            tasks.put(taskId, task);
            httpThreadPool.execute(task);
            WL.d(TAG, "Submit new task, id: " + taskId);
            return taskId;
        }
        return -1;
    }

    public void cancelTask(int taskId){
        WL.d(TAG, "Cancel old task, id: " + taskId);
        HttpEngineTask task = tasks.remove(taskId);
        if(task != null){
            task.cancel();
        }
    }

    protected int get(String url, AsyncResponseHandler handler) {
        ResponseHanderInterface handlerInterface = handler == null ? null : handler.getResponseHanderInterface();
        HttpEngineTask task = HttpEngineTask.createHttpGetEngineTask(url, handlerInterface);
        return submitTask(task);
    }

    protected int post(String url, PostRequestParams requestParams, AsyncResponseHandler handler) {
        ResponseHanderInterface handlerInterface = handler == null ? null : handler.getResponseHanderInterface();
        HttpEngineTask task = HttpEngineTask.createHttpPostEngineTask(url, requestParams, handlerInterface);
        return submitTask(task);
    }

    public int getPlatformConfig(String url, ConfigInfoRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }


    //    public int getAccountInfosByPhone(String url, AccountInfosRequestParams requestParams, AsyncResponseHandler handler){
//        return post(url, createCommonRequestParams(requestParams), handler);
//    }

    public int login(String url, LoginRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }
    public int autoLogin(String url, LoginAutoRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int loginByPhone(String url, LoginPhoneRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int quickRegister(String url, QuickRegisterRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int register(String url, RegisterRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int logout(String url, LogoutRequestParams requestParams, AsyncResponseHandler handler){
        return post(url, createCommonRequestParams(requestParams), handler);
    }

    public int sendPhoneVerify(String url, SendVerifyReuqestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int bindPhoneNumber(String url, BindPhoneReuqestParams requestParams, AsyncResponseHandler handler){
       return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }
    public int bindIDCard(String url, BindIDCardReuqestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int modifyPassword(String url, ModifyPasswordReuqestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int createOrder(String url, CreateOrderRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int getPayChannel(String url, PayChannelReuqestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

    public int uploadRoleInfo(String url, RoleInfoRequestParams requestParams, AsyncResponseHandler handler){
        return post(url+requestParams.getServiceString(), createCommonRequestParams(requestParams), handler);
    }

//    public int checkVersion(String url, CheckVersionRequestParams requestParams, AsyncResponseHandler handler){
//        return post(url, createCommonRequestParams(requestParams), handler);
//    }
//
//    public int getGameActivityInfo(String url, ActivityRequestParams requestParams, AsyncResponseHandler handler){
//        return post(url, createCommonRequestParams(requestParams), handler);
//    }
//
//    public int getGamePushInfo(String url, PushInfoRequestParams requestParams, AsyncResponseHandler handler){
//        return post(url, createCommonRequestParams(requestParams), handler);
//    }

//    public int reportVideoReward(String url, RewardVideoRequestParams requestParams, AsyncResponseHandler handler){
//        return post(url, createCommonRequestParams(requestParams), handler);
//    }

    private PostRequestParams createCommonRequestParams(BaseRequestParams requestParams){
        if(requestParams != null){
//            String ttChannel = WanSDK.getInstance().getTouTiaoChannel();
//            if(TextUtils.isEmpty(ttChannel)){
//                requestParams.getBaseFB().setGameInfo();
//                requestParams.setChannelId(XmlUtils.getSdkChannelId());
//            }else {
//                requestParams.setChannelId(ttChannel);
//            }
//            requestParams.setChannel_str(XmlUtils.getSdkChannelKey());
//            requestParams.setInner_sdk_version(XmlUtils.getSdkVersionName());
//            requestParams.setGameVersionCode(XmlUtils.getChannelGameVersionCode());
//            requestParams.setGameVersionName(XmlUtils.getChannelGameVersionName());
        }
        return PostRequestParams.createRequestParams(requestParams);
    }

}
